package com.cheeven.cwliu.citation.steps;

import java.util.HashMap;
import org.w3c.dom.Document;

public class Step1 extends StepBase {

	public static void establishHttpSession(HashMap<String, String> control) {
		logInfo("1. Load the first page and create the session");

		Document domdoc;

		String url = "http://apps.webofknowledge.com";
		String page = httpGet(url);
		domdoc = toDOM(page);

		String institutionalLoginPageUrl = "https://sub3.webofknowledge.com" + xpath("//a[contains(text(), 'Shibboleth')]/@href", domdoc);
		String institutionalLoginPage = httpGet(institutionalLoginPageUrl);
		domdoc = toDOM(institutionalLoginPage);

		String ukfederationPageUrl = xpath("//option[.='UK Federation']/@value", domdoc);
		String ukfederationPage = httpGet(ukfederationPageUrl);
		domdoc = toDOM(ukfederationPage);

		String samlRequestPageUrl = "https://wayf.ukfederation.org.uk/DS002/uk.ds?"
				+ "entityID=https%3A%2F%2Fsp.tshhosting.com%2Fshibboleth"
				+ "&returnX=https%3A%2F%2Fwww.webofknowledge.com%2F%3Fauth%3DShibbolethIdPForm%26target%3Dhttps%25253A%25252F%25252Fwww.webofknowledge.com%25252F%25253F%252526Error%25253DClient.NullSessionID%252526locale%25253Den_US%252526Alias%25253DWOK5%252526ShibFederation%25253DukFederation"
				+ "&returnIDParam=entityID"
				+ "&cache=perm"
				+ "&action=selection"
				+ "&origin=https%3A%2F%2Fidp.warwick.ac.uk%2Fidp%2Fshibboleth";
		/*
		String samlRequestPageUrl = "https://wayf.ukfederation.org.uk/DS002/uk.ds?"
				+ "entityID=" + xpath("//input[@name='entityID']/@value", domdoc)
				+ "&returnX=" + xpath("//input[@name='returnX']/@value", domdoc)
				+ "&returnIDParam=" + xpath("//input[@name='returnIDParam']/@value", domdoc)
				+ "&cache=" + xpath("//input[@name='cache']/@value", domdoc)
				+ "&action=" + xpath("//input[@name='action']/@value", domdoc)
				+ "&origin=" + xpath("//option[.='University of Warwick']/@value", domdoc);
				*/
		String samlRequestPage = httpGet(samlRequestPageUrl);
		domdoc = toDOM(samlRequestPage);

		String warwickLoginPage = httpPost(
				xpath("//form[@name='goto']/@action", domdoc),
				new String[][]{
					{"SAMLRequest", xpath("//input[@name='SAMLRequest']/@value", domdoc)},
					{"RelayState", xpath("//input[@name='RelayState']/@value", domdoc)}});
		domdoc = toDOM(warwickLoginPage);

		warwickLoginPage = httpPost(
				xpath("//form[@id='loginform']/@action", domdoc), 
				new String[][]{
					{"userName", control.get(getAppPrefix() + ".username")},
					{"password", control.get(getAppPrefix() + ".password")}});
		domdoc = toDOM(warwickLoginPage);

		warwickLoginPage = httpPost(
				xpath("//form[@id='shibboleth']/@action", domdoc), 
				new String[][]{
					{"TARGET", xpath("//input[@name='TARGET']/@value", domdoc)},
					{"SAMLResponse", xpath("//input[@name='SAMLResponse']/@value", domdoc)}});
		domdoc = toDOM(warwickLoginPage);

		warwickLoginPage = httpPost(xpath("//form/@action", domdoc), new String[][]{
					{"RelayState", xpath("//input[@name='RelayState']/@value", domdoc)},
					{"SAMLResponse", xpath("//input[@name='SAMLResponse']/@value", domdoc)}});
		domdoc = toDOM(warwickLoginPage);

		/*
		String initialPage = control.get(getAppPrefix() + ".initial.page");
		logInfo(1, "Initial Page is {0}", initialPage);

		String page1 = httpGet(initialPage);
		domdoc = toDOM(page1);
		*/
		
		control.put("sid", xpath("//input[@name='SID']/@value", domdoc));
		control.put("saParams", xpath("//input[@name='sa_params']/@value", domdoc));
		control.put("nextAction", "http://apps.webofknowledge.com" + xpath("//form[@id='UA_GeneralSearch_input_form']/@action", domdoc));

	}
}
